 truncate table asm_estratto_conto;
 select * from asm_estratto_conto;
 insert into asm_estratto_conto
 
 select ec.az_id,
 ec.MOV_NUM,
 ec.SCR_IMP_D,
 ec.SCR_IMP_A,
 ec.PAR_ID,
 ra.rata_num,
 ra.rata_imp,
        ec.SOG_ID,
        ec.CSI_IDCLI,
        ec.SOG_RAGSOC,
        ec.SCR_RIFERIMENTO,
        ec.ISS_ID_PARTITA,
        ec.DST_ID,
        ec.TSCR_DES,
        ra.rata_dtsca,
        
        ec.MOV_DT,
        ra.inc_dtpag
 
-- ec.*,to_char(ra.rata_dtsca,'dd/mm/yyyy') data_scadenza_rata
    from V_SCRITTURADEST_EC_tmp ec,v_rata ra
where ec.az_id = '01'
and   ra.az_id(+) = ec.az_id
and   ra.par_id(+)=ec.par_id 
and   ra.dil_staidsys(+) = 2
--and   ec.par_id = 389355
AND  EC.ISS_ID_PARTITA =602 
and  EC.MOV_DT > sysdate -120  --stato partita 602 aperta 603 chiusa
AND     EC.DST_ID in( 1,18)
and   ec.tmov_id= 1 

           
UNION 

 select ec.az_id,
 ec.MOV_NUM,
 ec.SCR_IMP_D,
 ec.SCR_IMP_A,
 ec.PAR_ID,
 ra.rata_num,
 ra.rata_imp,
        ec.SOG_ID,
        ec.CSI_IDCLI,
        ec.SOG_RAGSOC,
        ec.SCR_RIFERIMENTO,
        ec.ISS_ID_PARTITA,
        ec.DST_ID,
        ec.TSCR_DES,
        ra.rata_dtsca,
        
        ec.MOV_DT,
        ra.inc_dtpag
 
-- ec.*,to_char(ra.rata_dtsca,'dd/mm/yyyy') data_scadenza_rata
    from V_SCRITTURADEST_EC_tmp ec,v_rata ra
where ec.az_id = '01'
and   ra.az_id(+) = ec.az_id

and   ra.par_id(+)=ec.par_id 
and   ra.dil_staidsys(+) = 2
--and   ec.par_id = 389355
and   ec.tmov_id= 1 
AND  EC.ISS_ID_PARTITA =603 
and  EC.MOV_DT > sysdate -120  --stato partita 602 aperta 603 chiusa
AND     EC.DST_ID in( 1)
and (rata_num is null or rata_num =1)

union

select ec.az_id,
 ec.MOV_NUM,
 ec.SCR_IMP_D,
 ec.SCR_IMP_A,
 ec.PAR_ID,
 0,
 0,
        ec.SOG_ID,
        ec.CSI_IDCLI,
        ec.SOG_RAGSOC,
        ec.SCR_RIFERIMENTO,
        ec.ISS_ID_PARTITA,
        ec.DST_ID,
        ec.TSCR_DES,
        to_date ('','dd/mm/yyyy'),
        ec.MOV_DT,
        to_date ('','dd/mm/yyyy')


from V_SCRITTURADEST_EC_tmp ec
where ec.az_id = '01'
AND  EC.ISS_ID_PARTITA =602 
and  EC.MOV_DT > sysdate -120  --stato partita 602 aperta 603 chiusa
AND     EC.DST_ID in( 1,18)
and   ec.tmov_id<> 1;
